﻿using Bilibili.Common.Api;
using Bilibili.Model.Resource;
using Bilibili.Model.Video;
using PCLUntils.Json;
using PCLUntils.Objects;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Bilibili.WebClient.Api
{
    public sealed class WatchLaterClient : ClientBase
    {
        public async Task<ReturnModel> Add(string aid)
        {
            try
            {
                DataInfo dataInfo = new DataInfo($"aid={aid}", ApiProviderBase.Build6);
                string results = await ApiClient.PostResults("https://api.bilibili.com/x/v2/history/toview/add", dataInfo.ToString());
                var json = results.ToJObject();
                if (json.IsNotEmpty())
                {
                    if (json["code"].ToInt32() == 0)
                        return BuildSuccessResult();
                    else
                        return BuildFailedResult(json["message"].ToString());
                }
                else
                    return BuildFailedResult();
            }
            catch (Exception ex)
            {
                return BuildExceptionResult(ex);
            }
        }
        public async Task<ReturnModel<List<WatchlaterItemModel>>> List(int pageIndex)
        {
            try
            {
                ApiInfo apiInfo = new ApiInfo("https://api.bilibili.com/x/v2/history/toview", $"&pn={pageIndex}&ps=100", true, ApiProviderBase.Build6);
                string results = await ApiClient.GetResults(apiInfo);
                var m = results.ToJObject();
                var model = m["data"]["list"].ToObject<List<WatchlaterItemModel>>();
                return BuildSuccessResult(model);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<List<WatchlaterItemModel>>(ex);
            }
        }
        public async Task<ReturnModel> Clear()
        {
            try
            {
                DataInfo dataInfo = new DataInfo(string.Empty, ApiProviderBase.Build6);
                string results = await ApiClient.PostResults("https://api.bilibili.com/x/v2/history/toview/clear", dataInfo.ToString());
                var json = results.ToJObject();
                if (json.IsNotEmpty())
                {
                    if (json["code"].ToInt32() == 0)
                        return BuildSuccessResult();
                    else
                        return BuildFailedResult(json["message"].ToString());
                }
                else
                    return BuildFailedResult();
            }
            catch (Exception ex)
            {
                return BuildExceptionResult(ex);
            }
        }
        public async Task<ReturnModel> Del()
        {
            try
            {
                DataInfo dataInfo = new DataInfo("viewed=true", ApiProviderBase.Build6);
                string results = await ApiClient.PostResults("https://api.bilibili.com/x/v2/history/toview/del", dataInfo.ToString());
                var json = results.ToJObject();
                if (json.IsNotEmpty())
                {
                    if (json["code"].ToInt32() == 0)
                        return BuildSuccessResult();
                    else
                        return BuildFailedResult(json["message"].ToString());
                }
                else
                    return BuildFailedResult();
            }
            catch (Exception ex)
            {
                return BuildExceptionResult(ex);
            }
        }
        public async Task<ReturnModel> Del(string id)
        {
            try
            {
                DataInfo dataInfo = new DataInfo($"aid={id}", ApiProviderBase.Build6);
                string results = await ApiClient.PostResults("https://api.bilibili.com/x/v2/history/toview/del", dataInfo.ToString());
                var json = results.ToJObject();
                if (json.IsNotEmpty())
                {
                    if (json["code"].ToInt32() == 0)
                        return BuildSuccessResult();
                    else
                        return BuildFailedResult(json["message"].ToString());
                }
                else
                    return BuildFailedResult();
            }
            catch (Exception ex)
            {
                return BuildExceptionResult(ex);
            }
        }
    }
}